//
//  CalendarView.m
//  Demo
//
//  Created by LG on 2018/10/16.
//  Copyright © 2018 LG. All rights reserved.
//

#import "CalendarView.h"

@interface CalendarView ()

@property (nonatomic,strong)UILabel *lbl_title;
@property (nonatomic,strong)UILabel *lbl_MainTitle;
@property (nonatomic,strong)UIButton *btn_close;
@property (nonatomic,strong)UIView *view_line;
@property (nonatomic,strong)UIButton *btn_checkin;

@end

@implementation CalendarView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self buildUI];
    }
    return self;
}

- (void)buildUI
{
    self.backgroundColor = ssRGBAlpha(251, 212, 153, 1);
    self.layer.cornerRadius = 5;
    self.layer.masksToBounds = YES;
    
    CGFloat y = 20.;
    
    CGFloat mainTitleWidth = CGRectGetWidth(self.lbl_MainTitle.frame);
    CGFloat mainTitleHeight = CGRectGetHeight(self.lbl_MainTitle.frame);
    CGFloat mainTitleY = 10;
    CGFloat mainTitleX = (CGRectGetWidth(self.bounds)-mainTitleWidth)/2.;
    self.lbl_MainTitle.frame = CGRectMake(mainTitleX, mainTitleY, mainTitleWidth, mainTitleHeight);
    
    CGRect lblTitleBounds = self.lbl_title.bounds;
    lblTitleBounds.origin = CGPointMake(15., y);
    self.lbl_title.frame = lblTitleBounds;
    
    CGRect btnCloseBounds = self.btn_close.bounds;
    btnCloseBounds.origin.x = CGRectGetWidth(self.bounds) - CGRectGetWidth(btnCloseBounds) - 30;
    btnCloseBounds.origin.y = y;
    btnCloseBounds.size.width += 10;
    self.btn_close.frame = btnCloseBounds;
    
    CGFloat btnWidth = 210.;
    CGFloat btnHeight = 40.;
    CGFloat containerWidth = CGRectGetWidth(self.bounds);
    CGFloat btnY = [[self class] viewHeightWithFold:NO]-20.-btnHeight;
    self.btn_checkin.frame = CGRectMake((containerWidth - btnWidth)/2., btnY, btnWidth, btnHeight);
    
    [self addSubview:self.lbl_MainTitle];
    [self addSubview:self.lbl_title];
    [self addSubview:self.btn_close];
    [self addSubview:self.view_line];
    [self addSubview:self.btn_checkin];
}

+ (CGFloat)viewHeightWithFold:(BOOL)fold
{
    if (fold) {
        return 30.;
    } else {
        return 169.;
    }
}

- (void)animateWithFold:(BOOL)fold
{
    if (self.foldCallBack) {
        self.foldCallBack(fold);
    }
    
    CGFloat containerWidth = CGRectGetWidth(self.bounds);
    CGFloat moveDistance = containerWidth/7.;
    CGFloat titleDistance = fold ? moveDistance:-moveDistance;
    CGFloat closeDistance = fold ? -moveDistance:moveDistance;
    CGFloat titleAlpha = fold ? 0:1;
    CGFloat closeAlpha = fold ? 0:1;
    CGFloat mainTitleAlpha = fold ? 1:0;
    
    [UIView animateWithDuration:.4 animations:^{
        self.lbl_title.frame = CGRectOffset(self.lbl_title.frame, titleDistance, 0);
        self.lbl_title.alpha = titleAlpha;
        self.btn_close.frame = CGRectOffset(self.btn_close.frame, closeDistance, 0);
        self.btn_close.alpha = closeAlpha;
        self.lbl_MainTitle.alpha = mainTitleAlpha;
    } completion:^(BOOL finished) {
        
    }];
}

#pragma mark Action
- (void)btnClick_display:(UIButton*)btn
{
    BOOL isDisplayed = !btn.selected;
    btn.selected = !isDisplayed;
    
    [self animateWithFold:!btn.selected];
    
    UIControlState controlState = btn.state;
    UIImage *img = [btn imageForState:controlState];
    [btn.titleLabel sizeToFit];
    [btn sizeToFit];
    
    btn.titleEdgeInsets = UIEdgeInsetsMake(0, -img.size.width, 0,img.size.width);
    btn.imageEdgeInsets = UIEdgeInsetsMake(0, btn.titleLabel.frame.size.width+5, 0, -btn.titleLabel.frame.size.width);
}

- (void)btnClick_mainTitle:(UIGestureRecognizer*)ges
{
    [self animateWithFold:NO];
}

#pragma mark -- Getter
- (UILabel *)lbl_title
{
    if (!_lbl_title) {
        _lbl_title = [[UILabel alloc]init];
        _lbl_title.textColor = [UIColor whiteColor];
        _lbl_title.font = [UIFont boldSystemFontOfSize:13];
        _lbl_title.text = @"签到日历";
        [_lbl_title sizeToFit];
    }
    
    return _lbl_title;
}

- (UILabel *)lbl_MainTitle
{
    if (!_lbl_MainTitle) {
        _lbl_MainTitle = [[UILabel alloc]init];
        _lbl_MainTitle.textColor = [UIColor whiteColor];
        _lbl_MainTitle.font = [UIFont boldSystemFontOfSize:15];
        _lbl_MainTitle.text = @"明日签到得1积分";
        [_lbl_MainTitle sizeToFit];
        _lbl_MainTitle.alpha = 0.;
        _lbl_MainTitle.userInteractionEnabled = YES;
        UITapGestureRecognizer *tapGes = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(btnClick_mainTitle:)];
        [_lbl_MainTitle addGestureRecognizer:tapGes];
    }
    
    return _lbl_MainTitle;
}

- (UIButton *)btn_close
{
    if (!_btn_close) {
        _btn_close = [UIButton buttonWithType:UIButtonTypeCustom];
        _btn_close.titleLabel.font = [UIFont boldSystemFontOfSize:12];
        [_btn_close setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_btn_close setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
        [_btn_close setTitle:@"收起" forState:UIControlStateNormal];
        [_btn_close setTitle:@"展开" forState:UIControlStateSelected];
        [_btn_close setImage:[UIImage imageNamed:@"jifen_shouqi"] forState:UIControlStateNormal];
        [_btn_close setImage:[UIImage imageNamed:@"jifen_zhankai"] forState:UIControlStateSelected];
        [_btn_close addTarget:self action:@selector(btnClick_display:) forControlEvents:UIControlEventTouchUpInside];
        [_btn_close.titleLabel sizeToFit];
        
        UIControlState controlState = _btn_close.state;
        UIImage *img = [_btn_close imageForState:controlState];
        [_btn_close.titleLabel sizeToFit];
        [_btn_close sizeToFit];
        
        _btn_close.titleEdgeInsets = UIEdgeInsetsMake(0, -img.size.width, 0,img.size.width);
        _btn_close.imageEdgeInsets = UIEdgeInsetsMake(0, _btn_close.titleLabel.frame.size.width+5, 0, -_btn_close.titleLabel.frame.size.width);
        
    }
    
    return _btn_close;
}

- (UIView *)view_line
{
    if (!_view_line) {
        _view_line = [UIView new];
        _view_line.backgroundColor = [UIColor colorWithRed:238/25 green:183/255 blue:94/255 alpha:1];
    }
    
    return _view_line;
}

- (UIButton *)btn_checkin
{
    if (!_btn_checkin) {
        _btn_checkin = [UIButton buttonWithType:UIButtonTypeCustom];
        [_btn_checkin setTitleColor:ssRGBAlpha(226, 180, 111, 1) forState:UIControlStateNormal];
        [_btn_checkin setTitle:@"签到" forState:UIControlStateNormal];
        [_btn_checkin setBackgroundColor:[UIColor whiteColor]];
        _btn_checkin.layer.cornerRadius = 5;
        _btn_checkin.layer.masksToBounds = YES;
    }
    
    return _btn_checkin;
}

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    CGRect foldBtnFrame = self.btn_close.frame;
    foldBtnFrame = [self convertRect:self.btn_close.frame toView:self];
//    if (
//    CGRectContainsPoint(foldBtnFrame, point)) {
        return self.btn_close;
//    }
//    UIView *view = [super hitTest:point withEvent:event];
//    return view;
}

@end
